<template>
 <van-datetime-picker
    v-model="currentDate"
    type="date"
    title="选择年月日"
    :min-date="minDate"
    :max-date="maxDate"
    @cancel="$emit('close')"
    @confirm="submit"
  />
</template>

<script>
import { updateUserProfile } from '@/api/user'
  import dayjs from 'dayjs'
  export default {
    name:'EditBirthday',
    data() {
      return {
        minDate: new Date(1949, 9, 1),
        maxDate: new Date(2049, 9, 1),
        currentDate:  new Date(this.birthday),
      }
    },
    props:['birthday'],
    methods: {
      async submit(val) {
        // 处理格式
        let birthday = dayjs(val).format('YYYY-MM-DD')
        try {
          const { data:res } = await updateUserProfile({
            birthday
          })
          this.$toast.success('更新成功')
          // 更新视图
          this.$emit('close',res.data.birthday)
        } catch (error) {
          this.$toast.fail('更新失败')
        }
      }
    },
  }
</script>

<style lang="less" scoped>

</style>